const categoryService = require('../services/category');
const categoryCode = require('../codes/category');

module.exports = {

	/**
	 * [addCategory 新增直播分类]
	 * @param  {[type]} name   			[分类名]
	 * @param  {[type]} parentId    	[父分类id]
	 * @return {[type]}             [description]
	 */
	async addCategory(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '增加分类失败',
			data: null,
			code: ''
		};
		let parentId = formData.parentId;

		let categoryResult = await categoryService.addCategory({
			name: formData.name,
			create_time: new Date().getTime(),
			update_time: new Date().getTime()
		}, parentId);
		if(categoryResult) {
			result.success = true;
			result.data = categoryResult;
			result.message = '增加分类成功';
		}
		ctx.body = result;
	},

	/**
	 * [getCategory 查询直播分类]
	 * @param  {[type]} id   			[分类id]
	 * @param  {[type]} name   			[分类名]
	 * @param  {[type]} parentId    	[父分类id]
	 * @return {[type]}             [description]
	 */
	async getCategory(ctx) {
		let formData = ctx.request.query;
		let result = {
			success: false,
			message: '查询分类失败',
			data: null,
			code: ''
		};

		let categoryResult = await categoryService.getCategory({
			id: formData.id,
			name: formData.name,
			parent_id: formData.parentId
		});
		if(categoryResult) {
			result.success = true;
			result.data = categoryResult;
			result.message = '查询分类成功';
		}
		ctx.body = result;
	},

	/**
	 * [updateCategory 更新直播分类]
	 * @param  {[type]} id   			[分类id]
	 * @param  {[type]} name   			[分类名]
	 * @param  {[type]} parentId    	[父分类id]
	 * @return {[type]}             [description]
	 */
	async updateCategory(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '更新分类失败',
			data: null,
			code: ''
		};
		let id = formData.id;

		let categoryResult = await categoryService.updateCategory({
			name: formData.name,
			parent_id: formData.parentId,
			create_time: new Date().getTime(),
			update_time: new Date().getTime()
		}, id);
		if(categoryResult) {
			result.success = true;
			result.data = categoryResult;
			result.message = '更新分类成功';
		}
		ctx.body = result;
	},

	/**
	 * [deleteCategory 删除直播分类]
	 * @param  {[type]} id   			[分类id]
	 * @return {[type]}             [description]
	 */
	async deleteCategory(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '删除分类失败',
			data: null,
			code: ''
		};

		let categoryResult = await categoryService.deleteCategory({
			id: formData.id,
		});
		if(categoryResult) {
			result.success = true;
			result.data = categoryResult;
			result.message = '删除分类成功';
		}
		ctx.body = result;
	},

	/**
	 * [getCategory 查询直播热门分类]
	 * @return {[type]}             [description]
	 */
	async getHotCategory(ctx) {
		let result = {
			success: false,
			message: '查询热门分类失败',
			data: null,
			code: ''
		};

		let categoryResult = await categoryService.getHotCategory();
		if(categoryResult) {
			result.success = true;
			result.data = categoryResult;
			result.message = '查询热门分类成功';
		}
		ctx.body = result;
	},
};